package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dao.IndexDao;
import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.model.Notice;
import cn.edu.cqupt.cxcy.model.Noticetype;
import cn.edu.cqupt.cxcy.util.Tools;
//import net.sf.ehcache.Cache;
//import net.sf.ehcache.CacheManager;
//import net.sf.ehcache.Element;

/**
 * 主页
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class IndexPage extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String pjax_back = "$(function(){$(\"a.pjax[href='index']\").trigger('click');});";
		if(req.getHeader("x-pjax") == null || req.getHeader("x-pjax").equals("null")) {
			req.setAttribute("pjax_back", pjax_back);
			List<Noticetype> nt = new IndexImpl().noticeTypes();
			req.setAttribute("nt", nt);
			req.getRequestDispatcher("html/index.jsp").forward(req, resp);
			return;
		} else if (req.getHeader("x-pjax").equals("true")) {
//			CacheManager manager = CacheManager.create();
//			Cache cache = manager.getCache("CxcyCache");
			IndexDao index  = new IndexImpl();
//			if(cache.get("notices") == null) {
				List<String> ness = Tools.int2String(index.getCountNess()); 
				List<Object> newpro = index.getNewProList();
				List<Object> successPro = index.getNewSuccessProList();
				Notice fnew = index.getNewNoticeHaveImg();
				List<String> imgNew = new ArrayList<>();
				List<Object> notices2 = new ArrayList<>();
				List<Object> notices3 = new ArrayList<>();
				List<Object> notices1 = new ArrayList<>();
				imgNew.add(fnew.getN_title()); //标题
				imgNew.add(Tools.getImgSrc(fnew.getN_content())); //图片
				imgNew.add(Tools.Html2Text(fnew.getN_content(), 200)); //内容
				imgNew.add(String.valueOf(fnew.getN_id()));
				notices2 = index.getNewNoticeList( fnew.getN_id(), 2); //创业新闻
				notices3 = index.getNewNoticeList( fnew.getN_id(), 3); //通知公告
				notices1 = index.getNewNoticeList( fnew.getN_id(), 1); //关于创业
//				synchronized (this) {
//					Element element = new Element("ness", ness);
//					cache.put(element);
//					element = new Element("newpro", newPro);
//					cache.put(element);
//					element = new Element("successPro", successPro);
//					cache.put(element);
//					element = new Element("imgNew", imgNew);
//					cache.put(element);
//					element = new Element("notices", notices);
//					cache.put(element);
//				}

//			}
			//			主页上 [关于创业],？团队？导师？投资人？融资
//			req.setAttribute("ness", cache.get("ness").getObjectValue());
//			//			主页上[最新创业项目](4条数据)
//			req.setAttribute("newpro", cache.get("newpro").getObjectValue());
//			//			主页上[成功案例](八条数据)
//			req.setAttribute("successPro", cache.get("successPro").getObjectValue());
//			//			主页上[新闻动态](有图片的1条数据)
//			req.setAttribute("imgNew",cache.get("imgNew").getObjectValue());
//			//			主页上[新闻动态](有图片的4条数据)
//			req.setAttribute("notices",cache.get("notices").getObjectValue());
		
			
			req.setAttribute("ness", ness);
			//			主页上[最新创业项目](4条数据)
			req.setAttribute("newpro", newpro);
			//			主页上[成功案例](八条数据)
			req.setAttribute("successPro", successPro);
			//			主页上[新闻动态](有图片的1条数据)
			req.setAttribute("imgNew", imgNew);
			//			主页上[新闻动态](有图片的4条数据)
			req.setAttribute("notices2", notices2);
			req.setAttribute("notices3", notices3);
			req.setAttribute("notices1", notices1);
			req.getRequestDispatcher("html/index_body.jsp").forward(req, resp);
			return;
		}
	}
}
